]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapRender/MapVertices.swift
Address the lint warnings
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapVertices.swift
1 // Copyright (C) 2024 Rubén Beltrán del Río
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see https://map.tranquil.systems.
15 import SwiftUI
16
17 struct MapVertices: View {
18
19 let mapSize: CGSize
20 let vertexSize: CGSize
21 let vertices: [Vertex]
22 let padding = CGFloat(5.0)
23
24 var onDragVertex: (Vertex, CGFloat, CGFloat) -> Void = { _, _, _ in }
25
26 var body: some View {
27 ZStack(alignment: .topLeading) {
28 ForEach(vertices, id: \.id) { vertex in
29 ZStack(alignment: .topLeading) {
30 getVertexShape(vertex).fill(Color.Map.vertexColor)
31 Text(vertex.label.replacingOccurrences(of: "\\n", with: "\n")).font(.Theme.vertexLabel)
32 .foregroundColor(.Map.labelColor)
33 .shadow(color: .white, radius: 0, x: -0.5, y: -0.5)
34 .shadow(color: .white, radius: 0, x: 0.5, y: 0.5)
35 .offset(
36 CGSize(
37 width: w(vertex.position.x) + vertexSize.width + padding,
38 height: h(vertex.position.y) + 7.0))
39 }.gesture(
40 DragGesture()
41 .onChanged { value in
42 let deltaX = value.startLocation.x - value.location.x
43 let deltaY = value.startLocation.y - value.location.y
44 onDragVertex(vertex, deltaX, deltaY)
45 }
46 )
47 }
48 }
49 }
50
51 func h(_ dimension: CGFloat) -> CGFloat {
52 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
53 }
54
55 func w(_ dimension: CGFloat) -> CGFloat {
56 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
57 }
58
59 func getVertexShape(_ vertex: Vertex) -> Path {
60 switch vertex.shape {
61 case .circle:
62 return Path { path in
63 path.addEllipse(
64 in: CGRect(
65 origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
66 ))
67 }
68 case .square:
69 return Path { path in
70 path.addRect(
71 CGRect(
72 x: w(vertex.position.x), y: h(vertex.position.y), width: vertexSize.width,
73 height: vertexSize.height
74 ))
75 }
76 case .triangle:
77 return Path { path in
78 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
79 path.addLine(
80 to: CGPoint(
81 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
82 )
83 path.addLine(
84 to: CGPoint(x: w(vertex.position.x) + vertexSize.width / 2.0, y: h(vertex.position.y)))
85 path.addLine(
86 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
87 path.closeSubpath()
88 }
89 case .x:
90 return Path { path in
91 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)))
92 path.addLine(
93 to: CGPoint(
94 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
95 )
96 path.closeSubpath()
97 path.move(to: CGPoint(x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y)))
98 path.addLine(
99 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
100 path.closeSubpath()
101 }.strokedPath(StrokeStyle(lineWidth: 2.0, lineCap: .butt))
102 }
103 }
104 }
105
106 #Preview {
107 MapVertices(
108 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
109 vertices: [
110 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
111 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
112 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
113 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),
114 ])
115 }